package be.pluyckx.batteryprogress.utils.calculator;

import be.pluyckx.batteryprogress.data.history.BatteryHistory;
import be.pluyckx.batteryprogress.data.history.BatteryLevel;

/**
 * Created by Philip on 14/09/13.
 */
public class BatteryHistoryWrapper implements ProgressCalculator.DataCollection {
  private final BatteryHistory mBatteryHistory;

  public BatteryHistoryWrapper(BatteryHistory batteryHistory) {
    mBatteryHistory = batteryHistory;
  }

  @Override
  public ProgressCalculator.DataItem get(int index) {
    return new DataItemWrapper(mBatteryHistory.get(index));
  }

  @Override
  public ProgressCalculator.DataItem getFirstInCurrentState() {
    if (mBatteryHistory.size() > 0) {
      return new DataItemWrapper(mBatteryHistory.getFirstInCurrentState());
    } else {
      return null;
    }
  }

  @Override
  public ProgressCalculator.DataItem getLastInCurrentState() {
    if (mBatteryHistory.size() > 0) {
      return new DataItemWrapper(mBatteryHistory.getLastInCurrentState());
    } else {
      return null;
    }
  }

  @Override
  public int size() {
    return mBatteryHistory.size();
  }

  @Override
  public int getOrdered() {
    return ProgressCalculator.Parameters.ORDERED_OLD_LOWEST_INDEX;
  }

  private static class DataItemWrapper implements ProgressCalculator.DataItem {
    private final BatteryLevel mBatteryLevel;

    public DataItemWrapper(BatteryLevel batteryLevel) {
      mBatteryLevel = batteryLevel;
      if (mBatteryLevel == null) {
        throw new IllegalArgumentException("batteryLevel may not be null!");
      }
    }

    @Override
    public float getValue() {
      return mBatteryLevel.getBatteryLevelPercentage();
    }

    @Override
    public long getElapsed() {
      return mBatteryLevel.getElapsed();
    }

    @Override
    public long getDate() {
      return mBatteryLevel.getDate();
    }

    @Override
    public int getState() {
      return mBatteryLevel.getState();
    }
  }
}
